package web

import (
	"context"

	"portal/internal/errorx"
	"portal/internal/svc"
	"portal/internal/types"
	"portal/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListProductLogic struct {
	logx.Logger
	ctx          context.Context
	svcCtx       *svc.ServiceContext
	productModel model.AwProductsModel
}

func NewListProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListProductLogic {
	return &ListProductLogic{
		Logger:       logx.WithContext(ctx),
		ctx:          ctx,
		svcCtx:       svcCtx,
		productModel: model.NewAwProductsModel(svcCtx.Orm),
	}
}

func (l *ListProductLogic) ListProduct(req *types.PageParamRequest) (resp *types.CommonResponse, err error) {
	// todo: add your logic here and delete this line
	productDtos, err := l.productModel.ListByPage(l.ctx, nil, req.PageSize, req.PageNum)
	if err != nil {
		return nil, errorx.NewBizError(500, err.Error())
	}
	productVos := make([]*types.Product, 0)
	for i := 0; i < len(productDtos); i++ {
		productVos = append(productVos, &types.Product{
			Id:             productDtos[i].Id,
			ProductLogo:    productDtos[i].ProductLogo,
			ProductName:    productDtos[i].ProductName.String,
			ProductTitle:   productDtos[i].ProductTitle,
			ProductDesc:    productDtos[i].ProductDesc,
			ProductSubDesc: productDtos[i].ProductSubDesc.String,
			ProductLink:    productDtos[i].ProductLink.String,
			CoverImg:       productDtos[i].CoverImg,
			CreateTime:     productDtos[i].CreateTime,
			UpdateTime:     productDtos[i].UpdateTime,
		})
	}
	return &types.CommonResponse{
		Status: 200,
		Msg: "查询成功",
		Data: &types.ListProductResponse{
			Products: productVos,
		},
	 }, nil
}
